import { getSignService } from '@/api/login'
import * as ww from '@wecom/jssdk'

export async function getSign() {
  const url = location.href.split('#')[0]
  alert(url)
  const res = await getSignService({ url })
  console.log('jssdk', res)
  const { corpid, timestamp, nonceStr, configSignature, agentConfigSignature } = res
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // 该代码仅用于快速接入，请勿在生产环境对外暴露JSAPI_TICKET
  // Ticket有效期为2个小时，过期后请手动更换
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  const JSAPI_TICKET = 'LIKLckvwlJT9cWIhEQTwfJTxASwLxa_hQqsIfDoqOPd5ZAj47VJmfdsnlU7kYCT7yHetAqWdJnDHZj6ZD0dV8w'
  const AGENT_JSAPI_TICKET = 'LDS/eGLDs+OfntoISykTXg=='
  ww.register({
    corpId: corpid,
    jsApiList: ['chooseImage', 'getLocation', 'selectExternalContact'],
    getConfigSignature() {
      const obj = {
        ticket: JSAPI_TICKET,
        nonceStr,
        timestamp,
        url
      }
      const msg = JSON.stringify(ww.getSignature(obj))
      alert(msg)
      return ww.getSignature(obj)
    },
    // getAgentConfigSignature() {
    //   return ww.getSignature(AGENT_JSAPI_TICKET)
    // },
    onConfigSuccess: (res) => {
      const msg = 'onConfigSuccess success2' + JSON.stringify(res)
      alert(msg)
    },
    onConfigFail: (res) => {
      const msg = 'onConfigFail  failed2' + JSON.stringify(res)
      alert(msg)
    },
    // onAgentConfigSuccess: () => {
    //   alert('onAgentConfigSuccess success')
    // },
    // onAgentConfigFail: () => {
    //   alert('onAgentConfigSuccess failed')
    // },
    onConfigComplete: (res) => {
      const msg = 'onConfigComplete1' + JSON.stringify(res)
      alert(msg)
    }
    // onAgentConfigComplete: (res) => {
    //   console.log('onAgentConfigComplete', res)
    // }
  })
}
